import com.mysql.cj.jdbc.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * Creat with IntelliJ IDEA
 * Description
 * User: mamba24
 * Date: 2022-07-22
 * Time: 10:14
 * To BE A Better Person-2020-3-2
 */
public class JDBChomeworkd {
    public static void main(String[] args) throws SQLException {
        DataSource dataSource=new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/java104?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("lakers26");
        Connection connection=dataSource.getConnection();
        Scanner sc=new Scanner(System.in);
        int time=3;
        String sql1="insert into student values(?,?)";
        PreparedStatement statement=connection.prepareStatement(sql1);
        //新增三条数据
        while(time>0){
            String name=sc.next();
            int age=sc.nextInt();
            statement.setString(1,name);
            statement.setInt(2,age);
            int ret=statement.executeUpdate();
            System.out.println("新增数据"+ret+"条");
            time--;
        }

        //删除一条数据
        String sql2="delete from emp where name='krik'";
        PreparedStatement statement2=connection.prepareStatement(sql2);
        int ret=statement2.executeUpdate();
        System.out.println("删除数据"+ret+"条");

        //修改一条数据
        String sql3="update student set age=1 where name='dottie'";
        PreparedStatement statement3=connection.prepareStatement(sql3);
        int ret2=statement3.executeUpdate();
        System.out.println("修改数据"+ret2+"条");

        //查询数据
        String sql4="select * from student";
        PreparedStatement statement4=connection.prepareStatement(sql4);
        ResultSet resultSet=statement4.executeQuery();
        while(resultSet.next()){
            int id=resultSet.getInt("age");
            String name=resultSet.getString("name");
            System.out.println("age:"+id+";"+"name:"+name);
        }

        statement.close();
        statement2.close();
        statement3.close();
        statement4.close();
        connection.close();

    }


}
